package org.oschina.api.blog.recommend.service;

import java.util.List;

import org.oschina.Config;
import org.oschina.Constants;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.blog.blog.service.BlogService;
import org.oschina.api.blog.blog.vo.BlogVo;
import org.oschina.api.blog.recommend.dao.BlogRecommendDAO;
import org.oschina.api.blog.recommend.model.BlogRecommend;
import org.oschina.api.blog.recommend.vo.BlogRecommendVo;
import org.oschina.api.index.service.IndexService;
import org.oschina.api.manage.config.service.ConfigService;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.security.annotation.Require;
import org.oschina.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BlogRecommendService extends BaseService {

	@Autowired
	private BlogRecommendDAO blogRecommendDAO;
	@Autowired
	private BlogService blogService;
	@Autowired
	private IndexService indexService;
	@Autowired
	private ConfigService configService;
	
	/**
	 * 自荐
	 * @param id
	 * @return
	 */
	@Require
	public Result recommend(int id)
	{
		BlogVo blogVo=blogService._findVoById(id);
		if(blogVo==null || !blogService._isSimple(blogVo) || blogService._isRecommend(blogVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		blogService._require(getToken(), blogVo, new String[]{}, new String[]{Module.MODULE_61});
		
		BlogRecommend blogRecommend=new BlogRecommend();
		blogRecommend.setBid(id);
		return ResultUtil.getSuccess(_create(blogRecommend));
		
	}
	
	/**
	 * 返回
	 * @param pn
	 * @return
	 */
	@Require(authorization=Module.MODULE_62)
	public Pager<BlogRecommendVo> findVo(int pn)
	{
		Pager<BlogRecommendVo> pager=new Pager(pn,_getCount(),_getPageCount(),null);
		if(pager.getData_sum()>0)
			pager.setData(_findVo(pager.getStart(),pager.getPage_count()));
		
		return pager;
	}
	
	/**
	 * 通过
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_62)
	public Result pass(int id)
	{
		BlogRecommendVo blogRecommendVo=_findVoById(id);
		if(blogRecommendVo==null)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_delete(id);
		blogService._updateRecommendtime(blogRecommendVo.getBid(), DateUtil.now());
		return ResultUtil.getSuccess(Message.UPDATE_SUCCESS);
	}
	
	/**
	 * 失败
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_62)
	public Result refuse(int id)
	{
		if(_delete(id)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		return ResultUtil.getSuccess(Message.DELETE_SUCCESS);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	private BlogRecommendVo _findVoById(int id)
	{
		return blogRecommendDAO.findVoById(id);
	}
	
	/**
	 * 返回
	 * @param first
	 * @param max
	 * @return
	 */
	private List<BlogRecommendVo> _findVo(int first,int max)
	{
		return blogRecommendDAO.findVo(first, max);
	}
	
	/**
	 * 返回数量
	 * @return
	 */
	private int _getCount()
	{
		return blogRecommendDAO.getCount();
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	private int _delete(int id)
	{
		return blogRecommendDAO.delete(id);
	}
	
	/**
	 * 创建
	 * @param blogRecommend
	 * @return
	 */
	private BlogRecommend _create(BlogRecommend blogRecommend)
	{
		blogRecommend.setId(_getIndex());
		blogRecommend.setCreate_time(DateUtil.now());
		return blogRecommendDAO.create(blogRecommend);
	}
	
	private int _getPageCount()
	{
		return (Integer) configService.getValueById(Config.CONFIG_16, Integer.class);
	}
	
	private int _getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_BLOG_RECOMMEND);
	}
}